package com.kangye.learnstudent.controller;

import com.kangye.learnstudent.entity.User;
import com.kangye.learnstudent.service.UserService;
import com.kangye.learnstudent.util.RedisStorageUtil;
import com.kangye.learnstudent.util.ViewJsonObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @program: learndeduo
 * @description: 用户请求接口
 * @author: 高天乐
 * @create: 2021-05-17 13:58
 **/
@RestController
@CrossOrigin
@RequestMapping("/user")
public class UserController {
    //注入service层
    @Autowired
    private UserService userService;
    @Autowired
    private RedisStorageUtil redisStorageUtil;

    /**
     * 学生登录
     * @RequestMapping(value = "/login",method = RequestMethod.POST) 设置虚拟路径 post提交方式
     */
    @RequestMapping(value = "/login",method = RequestMethod.POST)
    public ViewJsonObject userLogin(String userUserName , String userPassword){
        //返回数据
        return userService.userLogin(userUserName,userPassword);
    }

    /**
     * 判断用户绑定的手机号,及给手机号发送短信
     * @RequestMapping(value = "/userPhoneMsg",method = RequestMethod.POST) 设置虚拟路径 post提交方式
     * @param userUserName
     * @param userPhone
     * @return
     */
    @RequestMapping(value = "/userPhoneMsg",method = RequestMethod.POST)
    public ViewJsonObject userPhoneMsg(String userUserName,String userPhone){
        //返回数据
        return userService.userPhoneMsg(userUserName,userPhone);
    }

    /**
     * 注册,手机号发送短信
     * @RequestMapping(value = "/userPhoneMsg",method = RequestMethod.POST) 设置虚拟路径 post提交方式
     * @param userPhone
     * @return
     */
    @RequestMapping(value = "/registerPhoneMsg",method = RequestMethod.POST)
    public ViewJsonObject<User> userPhoneMsg(String userPhone){
        //返回数据
        return userService.userPhoneMsg(userPhone);
    }

    /**
     * 重置密码接口
     * @RequestMapping(value = "/passwordReset",method = RequestMethod.POST) 设置虚拟路径 post提交方式
     * @param userUserName 用户名
     * @param userPassword 密码
     * @param code 验证码
     * @return
     */
    @RequestMapping(value = "/passwordReset",method = RequestMethod.POST)
    public ViewJsonObject passwordReset(String userUserName,String userPassword,String userPhone,String code){
        System.out.println("userUserName:"+userUserName+" userPassword:" + userPassword + " code:" + code);
        //返回数据
        return userService.passwordReset(userUserName,userPassword,userPhone,code);
    }

    /**
     * 上传图片的接口
     * @return
     */
    @RequestMapping(value = "/imgUpload",method = RequestMethod.POST)
    public ViewJsonObject imgUpload(MultipartFile file) throws IOException {
        return userService.imgUpload(file);
    }

    /**
     * 注册用户
     * @param user 传入的用户信息
     * @param code 验证码
     * @return
     */
    @RequestMapping(value = "/registerUser",method = RequestMethod.POST)
    public ViewJsonObject registerUser(User user,String code){
        return userService.registerUser(user,code);
    }

    /**
     * 据用户名查询
     * @param userUserName
     * @return
     */
    @RequestMapping(value = "/userByUserName",method = RequestMethod.POST)
    public ViewJsonObject userByUserName(String userUserName){
        return userService.userByUserName(userUserName);
    }

    //从redis里取用户的值
    @RequestMapping("/getUserRedis")
    public ViewJsonObject<User> getRedis(Model model){
        //从redis里取值
        User user = (User) redisStorageUtil.getKeyOne("studentUserRedisData");
//        用户id
        Integer userId = user.getUserId();
        //json 返回页面
        return userService.userSelect(userId);
    }
    //根据id查询个人信息
    @RequestMapping("/getInfoMationById")
    public ViewJsonObject<User> getInfoMationById(Integer userId)   {
        return userService.userSelect(userId);
    }

    //根据id修改用户信息
    @RequestMapping("/userUpdata")
    public String userUpdata(User user){
        Integer code = userService.userUpdata(user);
        if (code>0){

            return "200";
        }else {
            return "201";
        }
    }
}
